﻿#include "GraphicsValveItem.h"
#include <QPainter>
#include <QPen>

GraphicsValveItem::GraphicsValveItem()
{
    m_centrePoint = QPointF(0, 0);
    m_topPoints[0] = QPointF(-m_radius * 0.7, -m_radius);
    m_topPoints[1] = QPointF(m_radius * 0.7, -m_radius);

    m_bottomPoints[0] = QPointF(-m_radius * 0.7, m_radius);
    m_bottomPoints[1] = QPointF(m_radius * 0.7, m_radius);

    m_leftPoints[0] = QPointF(-m_radius, -m_radius * 0.7);
    m_leftPoints[1] = QPointF(-m_radius, m_radius * 0.7);
}

QRectF GraphicsValveItem::boundingRect() const
{
    //return QRectF(-25, -25, 40, 50) * scale();
    return QRectF(-m_radius * scale(), -m_radius * scale(),  1.7 * m_radius * scale(), 2 * m_radius * scale());
}

// void GraphicsValveItem::setScale(double scale)
// {
//     m_scale = scale;

//     m_topPoints[0] = QPointF(-15, -25) * m_scale;
//     m_topPoints[1] = QPointF(15, -25) * m_scale;

//     m_bottomPoints[0] = QPointF(-15, 25) * m_scale;
//     m_bottomPoints[1] = QPointF(15, 25) * m_scale;

//     m_leftPoints[0] = QPointF(-25, -15) * m_scale;
//     m_leftPoints[1] = QPointF(-25, 15) * m_scale;
// }

QPointF GraphicsValveItem::keyPoint(int i)
{
    switch(i)
    {
    case 0:
        //return QPointF(x(), -25 + y());
        return QPointF(x(), -m_radius * scale() + y());
        break;
    default:
        //return QPointF(x(), 25 + y());
        return QPointF(x(), m_radius * scale() + y());
        break;
    }
}

void GraphicsValveItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen(Qt::black);
    pen.setWidth(3);
    painter->setPen(pen);

    QBrush brushOn(Qt::green);
    painter->setBrush(brushOn);
    QPointF p[6] = {m_topPoints[0], m_topPoints[1], m_centrePoint, m_bottomPoints[1], m_bottomPoints[0], m_centrePoint};

    painter->drawPolygon(p, 6);
    // painter->drawLine(m_topPoints[0], m_topPoints[1]);
    // painter->drawLine(m_topPoints[0], m_centrePoint);
    // painter->drawLine(m_topPoints[1], m_centrePoint);

    // painter->drawLine(m_bottomPoints[0], m_bottomPoints[1]);
    // painter->drawLine(m_bottomPoints[0], m_centrePoint);
    // painter->drawLine(m_bottomPoints[1], m_centrePoint);

    painter->drawLine(m_leftPoints[0], m_leftPoints[1]);
    painter->drawLine((m_leftPoints[0] + m_leftPoints[1]) / 2.0, m_centrePoint);

}
